﻿using System;
using System.Collections.Generic;
using Cms.Utilities;

namespace Cms.Domain
{
    public class ContentTranslationVersion : IAuditable
    {
        private IList<DetailTemplateContentSet> _detailTemplateContentSets;
        private IList<MasterTemplateContentSet> _masterTemplateContentSets;
        private IList<PageContentSet> _pageContentSets;
        private IList<ScheduledPublishing> _scheduledPublishings;
        private string _timestamp;
        private ContentTranslation _translation;

        public ContentTranslationVersion()
        {
            _pageContentSets = new List<PageContentSet>();
            _masterTemplateContentSets = new List<MasterTemplateContentSet>();
            _detailTemplateContentSets = new List<DetailTemplateContentSet>();
            _scheduledPublishings = new List<ScheduledPublishing>();
        }
        
        public virtual int ID { get; set; }

        public virtual string Timestamp
        {
            get { return _timestamp; }
            set { _timestamp = value; }
        }
        
        public virtual string Html { get; set; }

        public virtual ContentTranslation Translation
        {
            get { return _translation; }
            set { _translation = value; }
        }
        
        public virtual IList<PageContentSet> PageContentSets
        {
            get { return _pageContentSets; }
            set { _pageContentSets = value; }
        }

        public virtual IList<MasterTemplateContentSet> MasterTemplateContentSets
        {
            get { return _masterTemplateContentSets; }
            set { _masterTemplateContentSets = value; }
        }
        
        public virtual IList<DetailTemplateContentSet> DetailTemplateContentSets
        {
            get { return _detailTemplateContentSets; }
            set { _detailTemplateContentSets = value; }
        }

        public virtual string Title { get; set; }

        public virtual Administrator Author { get; set; }

        public virtual IList<ScheduledPublishing> ScheduledPublishings
        {
            get { return _scheduledPublishings; }
            set { _scheduledPublishings = value; }
        }
        
        public virtual void Publish()
        {
            _translation.PublishVersion(_timestamp);
            if (!_translation.FirstPublished.HasValue)
                _translation.FirstPublished = DateTimeProvider.Current.Now;
            _translation.LatestPublished = DateTimeProvider.Current.Now;
            foreach (ContentSetContainer container in _translation.Content.GetContainers())
                container.PublishContentVersion(_translation.Locale.ID, this);
        }

        #region IAuditable Members

        public virtual string GetState()
        {
            return "Timestamp = " + _timestamp +
                   ", Title = " + Title +
                   ", Html = " + Html;
        }

        #endregion
    }
}